package com.app.myApp.control;

import com.app.myApp.dao.RobotDAO;
import com.app.myApp.dataobject.RobotDO;
import com.app.myApp.dataobject.UserDO;
import com.app.myApp.model.Response;
import com.app.myApp.model.Robot;
import com.app.myApp.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class RobotController {

    @Autowired
    private RobotDAO robotDAO;

    @PostMapping("/robots/addAssistant")
    public ResponseEntity<Response> addAssistant(@RequestBody Robot robot) {
        String name = robot.getName();
        String email = robot.getEmail();

        // 1. 校验参数
        if (!StringUtils.hasText(email) || !StringUtils.hasText(name)) {
            return new ResponseEntity<>(Response.error("邮箱或姓名不能为空"), HttpStatus.BAD_REQUEST);
        }

        RobotDO robotDO = new RobotDO();
        robotDO.setName(robot.getName());
        robotDO.setDescription(robot.getDescription());
        robotDO.setEmail(robot.getEmail());

        int row = robotDAO.insertRobot(robotDO);
        if (row>0){
            return new ResponseEntity<>(Response.success("新增机器人成功"),HttpStatus.OK);
        }
        return new ResponseEntity<>(Response.error("新增机器人失败"),HttpStatus.OK);
    }


    @PostMapping("/robots/getAssistant")
    public ResponseEntity<Response> getAssistant(@RequestBody Robot robot) {
        String email = robot.getEmail();

        // 1. 校验参数
        if (!StringUtils.hasText(email)) {
            return new ResponseEntity<>(Response.error("邮箱不能为空"), HttpStatus.BAD_REQUEST);
        }

        List<RobotDO> robotDOList = robotDAO.findRobotByEmail(email);
        if (!robotDOList.isEmpty()){
            return new ResponseEntity<>(Response.success(robotDOList),HttpStatus.OK);
        }
        return new ResponseEntity<>(Response.error("查询机器人失败"),HttpStatus.OK);
    }

    @PostMapping("/robots/updateAssistant")
    public ResponseEntity<Response> updateAssistant(@RequestBody Robot robot) {
        String name = robot.getName();
        // 1. 校验参数
        if (!StringUtils.hasText(name)) {
            return new ResponseEntity<>(Response.error("机器人名字不能为空"), HttpStatus.BAD_REQUEST);
        }

        RobotDO robotDO = new RobotDO();
        robotDO.setRobotId(robot.getRobotId());
        robotDO.setName(robot.getName());
        robotDO.setDescription(robot.getDescription());
        robotDO.setEmail(robot.getEmail());

        int row = robotDAO.updateRobot(robotDO);
        if (row>0){
            return new ResponseEntity<>(Response.success("修改机器人成功"),HttpStatus.OK);
        }
        return new ResponseEntity<>(Response.error("修改机器人失败"),HttpStatus.OK);
    }

    @PostMapping("/robots/deleteAssistant")
    public ResponseEntity<Response> deleteAssistant(@RequestBody Robot robot) {
        int row = robotDAO.deleteRobotById(robot.getRobotId());
        if (row>0){
            return new ResponseEntity<>(Response.success("修改机器人成功"),HttpStatus.OK);
        }
        return new ResponseEntity<>(Response.error("修改机器人失败"),HttpStatus.OK);
    }


}
